How to hide tabview in SwiftUI?

by lue.lemke , in category: Swift , a year ago

How to hide tabview in SwiftUI?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by mathias.schinner , a year ago

@lue.lemke 

To hide a TabView in SwiftUI, you can use the .hidden() modifier. This will make the TabView not visible, but it will still be part of the view hierarchy and will occupy space in the layout.


Here's an example of how you can use the .hidden() modifier to hide a TabView:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
struct ContentView: View {
    @State private var isTabViewHidden = true

    var body: some View {
        TabView {
            Text("Tab 1")
                .tabItem {
                    Image(systemName: "1.circle")
                    Text("Tab 1")
                }

            Text("Tab 2")
                .tabItem {
                    Image(systemName: "2.circle")
                    Text("Tab 2")
                }
        }
        .hidden(isTabViewHidden)
    }
}


In this example, the TabView will be hidden if the isTabViewHidden state variable is true, and it will be shown if it is false. You can toggle the value of this variable in response to user input or some other event to show or hide the TabView.


Alternatively, you can use the .opacity() modifier to make the TabView partially transparent, so that it is still visible but not fully opaque. This can be useful if you want to fade the TabView in or out.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
struct ContentView: View {
    @State private var tabViewOpacity = 1.0

    var body: some View {
        TabView {
            Text("Tab 1")
                .tabItem {
                    Image(systemName: "1.circle")
                    Text("Tab 1")
                }

            Text("Tab 2")
                .tabItem {
                    Image(systemName: "2.circle")
                    Text("Tab 2")
                }
        }
        .opacity(tabViewOpacity)
    }
}


In this example, the TabView will be fully opaque if the tabViewOpacity state variable is 1.0, and it will be partially transparent if it is set to a value between 0.0 and 1.0. You can animate the value of this variable to fade the TabView in or out.

Member

by natalia , 4 months ago

@lue.lemke 

To hide a TabView in SwiftUI, you can use the .hidden() modifier. This will make the TabView not visible, and it will also be excluded from the layout and view hierarchy. Here's an example of how to hide a TabView:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct ContentView: View {
    @State private var isTabViewHidden = true

    var body: some View {
        if isTabViewHidden {
            EmptyView()
        } else {
            TabView {
                Text("Tab 1")
                    .tabItem {
                        Image(systemName: "1.circle")
                        Text("Tab 1")
                    }

                Text("Tab 2")
                    .tabItem {
                        Image(systemName: "2.circle")
                        Text("Tab 2")
                    }
            }
        }
    }
}


In this example, the TabView will be hidden if the isTabViewHidden state variable is set to true. It will show an empty view instead. You can toggle the value of isTabViewHidden in response to user input or any other event to show or hide the TabView.


Note that the .hidden() modifier only hides the view, but it still occupies space in the layout. If you want to completely remove the TabView from the view hierarchy, you can conditionally render it using an if statement as shown in the example above.